package com.yjhealth.libs.core.core.activity;

import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.yjhealth.libs.core.R;
import com.yjhealth.libs.core.net.utils.NetworkErrorUtil;
import com.yjhealth.libs.core.toast.ToastUtil;
import com.yjhealth.libs.core.view.loadview.LoadViewHelper;

public abstract class CoreLoadViewActivity extends CoreLifeActivity{
    private LoadViewHelper loadViewHelper;
    private boolean isEmpty = true;

    protected abstract void onRefreshView();

    public boolean isEmpty() {
        return isEmpty;
    }

    public void setEmpty(boolean empty) {
        isEmpty = empty;
    }

    protected void initLoadView() {
        View base = findViewById(R.id.yjhealthCoreLoadLay);
        if (base != null) {
            loadViewHelper = new LoadViewHelper(this, base);
            loadViewHelper.setErrorClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRefreshView();
                }
            });
        }
    }

    
    public boolean isViewReplace() {
        if (loadViewHelper == null) {
            return false;
        }
        return loadViewHelper.isRellace();
    }

    //***************** restore **************
    public void restoreView() {
        isEmpty = false;
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.restore();
    }

    //***************** loading **************
    public void showLoadingView() {
        showLoadingView(0, null);
    }

    public void showLoadingView(@DrawableRes int resourceId, String text) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showLoading(resourceId, text);
    }

    //***************** empty **************
    public void showEmptyView() {
        showEmptyView(0, null);
    }

    public void showEmptyView(@DrawableRes int resourceId, String text) {
        isEmpty = true;
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmpty(resourceId, text);
    }

    //***************** error 单图片 **************
    public void showErrorViewSimple(String errorType, String errorMsg) {
        showErrorViewSimple(errorType, errorMsg, 0, null);
    }

    public void showErrorViewSimple(String errorType, String errorMsg, @DrawableRes int defRes, String defText) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showErrorSimple(errorType, defRes, defText);
    }

    public void showErrorViewSimpleReplace(String errorType, String errorMsg, @DrawableRes int resourceId, String text) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showErrorSimple(resourceId, text);
    }

    //***************** error 点击重试 **************
    public void showErrorView(String errorType, String errorMsg) {
        showErrorView(errorType, errorMsg, 0, null);
    }

    public void showErrorView(String errorType, String errorMsg, @DrawableRes int defRes, String defText) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(errorType, defRes, defText);
    }

    public void showErrorViewReplace(String errorType, String errorMsg, @DrawableRes int resourceId, String text) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(resourceId, text);
    }

    //******************* 自定义 *****************
    public void showView(@NonNull View view) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showView(view);
    }

    public void showViewById(int layId) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showViewById(layId);
    }
}
